package com.xy.p2p.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.common.page.Pagination;
import com.xy.common.util.ErFen;
import com.xy.core.service.impl.BaseServiceImpl;
import com.xy.core.util.BeanUtil;
import com.xy.p2p.dao.TmemberassetaccountDao;
import com.xy.p2p.dao.TmemberassetaccountdetailsDao;
import com.xy.p2p.dao.TtenderrecordDao;
import com.xy.p2p.dao.TtransferinfoDao;
import com.xy.p2p.dao.TtransferrecordDao;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.model.Ttransferrecord;
import com.xy.p2p.service.TtransferrecordService;

@Service
@Transactional
public class TtransferrecordServiceImpl extends BaseServiceImpl<Ttransferrecord> implements
		TtransferrecordService {
	private TtransferrecordDao ttransferrecordDao;

	@Autowired
	private TtransferinfoDao ttransferinfoDao;
	@Autowired
	private TmemberassetaccountDao tmemberassetaccountDao;
	@Autowired
	private TmemberassetaccountdetailsDao tmemberassetaccountdetailsDao;
	@Autowired
	private TtenderrecordDao ttenderrecordDao;
	
	// 充值方向 -1：借（出金），1：贷（入金）
	private final int TMEMBERASSETACCOUNTDETAIL_BORROWING = -1;
	// 交易类型1:充值,其他见数据字典
	private final int TMEMBERASSETACCOUNTDETAIL_TRANTYPE = 7;
	

	public TtransferrecordServiceImpl(TtransferrecordDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.ttransferrecordDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return ttransferrecordDao.checkExist("Ttransferrecord", paraName, paraValue);
	}

	@Override
	public List<Ttransferrecord> getTtransferrecord(Map<String, Object> map) {
		return ttransferrecordDao.getTtransferrecord(map);
	}
	@Override
	public Pagination getTtransferrecordByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return ttransferrecordDao.getTtransferrecordByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Ttransferrecord save(Ttransferrecord ttransferrecord) {
//		ttransferrecord.setModtime(new Date());
		if(ttransferrecord.getTrid()==null){
			//新增
			
			Ttransferinfo ttransferinfo = ttransferinfoDao.get(ttransferrecord.getTtransferinfo().getTiid());
			//判断购买金额不能超过可购买债权金额(总债权金额-已转让部分金额)
			if(ttransferrecord.getTrmoney()>ttransferinfo.getTrmoney()-(ttransferinfo.getTsfmoney()==null?0:ttransferinfo.getTsfmoney())){
				return null;
			}
			
			ttransferrecord.setTtransferinfo(ttransferinfo);
			ttransferrecord.setTmemberByTrmemberid(ttransferinfo.getTmember());
			ttransferrecord.setTrfactor(ttransferinfo.getTrfactor());
			ttransferrecord.setTrdatetime(new Date());
			double trprice = 0;
			//计算购买价格 begin
			Ttenderrecord ttenderrecord = ttransferinfo.getTtenderrecord();
			//下一还款日
			Date d = ttenderrecord.getTfinanceinfo().getNrdate();
			//计算本金
			double p = ttransferrecord.getTrmoney();
			//年利率
			double r = ttenderrecord.getFinancepa();
			trprice =this.getTrprice(d,p,r);
			//计算购买价格 end
			//购买价格=(本金+日利息)X转让系数
			ttransferrecord.setTrprice((trprice+p)*ttransferinfo.getTrfactor());
			//判断余额是否足够支付
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("Tmember", ttransferrecord.getTmemberByAcmemberid());
			Tmemberassetaccount tmemberassetaccount = tmemberassetaccountDao.getTmemberassetaccount(queryMap).get(0);
			if(tmemberassetaccount.getBalance()<ttransferrecord.getTrprice()){
				return null;
			}
			ttransferrecord = super.save(ttransferrecord);
			//修改转让主表
//			ttransferinfo.setTrmoney(ttransferinfo.getTrmoney()-ttransferrecord.getTrmoney());
			ttransferinfo.setTsfmoney(ttransferinfo.getTsfmoney()==null?0:ttransferinfo.getTsfmoney()+ttransferrecord.getTrmoney());
			ttransferinfoDao.save(ttransferinfo);
			//修改投资标的计息金额
			ttenderrecord.setMibmoney(ttenderrecord.getMibmoney()-ttransferrecord.getTrmoney());
			ttenderrecordDao.save(ttenderrecord);
			//增加投标记录
			Ttenderrecord ttenderrecord2 = new Ttenderrecord(ttenderrecord.getTfinanceinfo(),ttransferrecord.getTmemberByAcmemberid()
					,ttransferrecord.getTrmoney(),new Date(),null,ttenderrecord.getFinancepa(),ttransferrecord.getTrmoney(),0,null,null,null);
			ttenderrecordDao.save(ttenderrecord2);
			//修改余额
			tmemberassetaccount.setBalance(tmemberassetaccount.getBalance()-ttransferrecord.getTrprice());
			tmemberassetaccountDao.save(tmemberassetaccount);
			//添加账户变动明细
			Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(ttransferrecord.getTmemberByAcmemberid(), ttransferrecord.getTrprice(),
					TMEMBERASSETACCOUNTDETAIL_BORROWING,TMEMBERASSETACCOUNTDETAIL_TRANTYPE , new Date(), ttransferinfo.getTmember().getLoginname(), null);
			tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
        	return ttransferrecord;
		}else{
			//更新
			Ttransferrecord ttransferrecord1=ttransferrecordDao.get(ttransferrecord.getTrid());
			try {
				BeanUtil.copyNotNullProperties(ttransferrecord1, ttransferrecord);
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return super.save(ttransferrecord1);
		}
	}

   //债权转让购买确认
	@Override
	public Ttransferrecord preSubmit(Ttransferrecord ttransferrecord) {
		Ttransferinfo ttransferinfo = ttransferinfoDao.get(ttransferrecord.getTtransferinfo().getTiid());
		double trprice = 0;
		Ttenderrecord ttenderrecord = ttransferinfo.getTtenderrecord();
		//下一还款日
		Date d = ttenderrecord.getTfinanceinfo().getNrdate();
		//计算本金
		double p = ttransferrecord.getTrmoney();
		//年利率
		double r = ttenderrecord.getFinancepa();
		trprice =this.getTrprice(d,p,r);
//		ttransferinfo.setTrmoney(ttransferrecord.getTrmoney());
//		ttransferinfo.setTrprice(trprice);
		//购买价格=(本金+日利息)X转让系数
		ttransferrecord.setTrprice((trprice+p)*ttransferinfo.getTrfactor());
		ttransferrecord.setTtransferinfo(ttransferinfo);
		ttransferrecord.setTmemberByTrmemberid(ttransferinfo.getTmember());
//		ttransferrecord.setTrmoney(ttransferinfo.getTrmoney());
//		ttransferrecord.setTrfactor(ttransferinfo.getTrfactor());
//		ttransferrecord.setTrreason(ttransferinfo.getTrreason());
//		ttransferrecord.setTrdatetime(ttransferinfo.getTrdatetime());
		return ttransferrecord;
	}
   
	//计算转让价格
	private double getTrprice(Date d,double p,double r){
		double result =0;
		result = ErFen.culPrice(p,r,d);
		 BigDecimal mData = new BigDecimal(result).setScale(2, BigDecimal.ROUND_HALF_UP);
		 return mData.doubleValue();
	}
	
}